﻿// Copyright (c) 2014 Converter Systems LLC

using Opc.Ua;
using System;
using System.Threading.Tasks;

namespace ConverterSystems.UaBrowser.ViewModels
{
    public class ReferenceDescriptionViewModel : TreeViewItemViewModel
    {
        private readonly ReferenceDescription _description;
        private readonly Type _dataType;
        private readonly byte _accessLevel;
        private readonly byte _notifier;
        private readonly Func<ReferenceDescriptionViewModel, Task> _loadChildren;

        public ReferenceDescriptionViewModel(ReferenceDescription description, Type dataType, byte accessLevel, byte eventNotifier, TreeViewItemViewModel parentViewModel, Func<ReferenceDescriptionViewModel, Task> loadChildren)
            : base(parentViewModel, (loadChildren != null))
        {
            _description = description;
            _dataType = dataType;
            _accessLevel = accessLevel;
            _notifier = eventNotifier;
            _loadChildren = loadChildren;
        }

        public Type DataType
        {
            get { return _dataType; }
        }

        public byte AccessLevel
        {
            get { return _accessLevel; }
        }

        public bool IsObject
        {
            get { return _description.NodeClass == NodeClass.Object; }
        }

        public bool IsMethod
        {
            get { return _description.NodeClass == NodeClass.Method; }
        }

        public bool IsVariable
        {
            get { return _description.NodeClass == NodeClass.Variable; }
        }

        public bool IsEventNotifier
        {
            get { return (_notifier & EventNotifiers.SubscribeToEvents) == EventNotifiers.SubscribeToEvents; }
        }

        public new ReferenceDescriptionViewModel Parent
        {
            get { return base.Parent as ReferenceDescriptionViewModel; }
        }

        public string DisplayName
        {
            get { return _description.DisplayName.ToString(); }
        }

        public ExpandedNodeId NodeId
        {
            get { return _description.NodeId; }
        }

        public QualifiedName BrowseName
        {
            get { return _description.BrowseName; }
        }

        public NodeClass NodeClass
        {
            get { return _description.NodeClass; }
        }

        protected override async Task LoadChildrenAsync()
        {
            await _loadChildren(this);
        }
    }

}